package com.example.neusoft.yuejian_12.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft.yuejian_12.Activity.ClubDetailActivity;
import com.example.neusoft.yuejian_12.Activity.PhotoInfoActivity;
import com.example.neusoft.yuejian_12.JavaBean.Photo;
import com.example.neusoft.yuejian_12.JavaBean.UserCollect;
import com.example.neusoft.yuejian_12.Model.UserModel;
import com.example.neusoft.yuejian_12.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 晓真 on 2017/10/24.
 */

public class UserPhotoListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;//承载上下文
    private List<Photo> mDataList;//动态数组承载数据使用 数据存储
    private LayoutInflater mLayoutInflater; //准备布局
    private String userid;

//    private String user_name,user_photo;

    public UserPhotoListAdapter(Context mContext, List<Photo> mDataList, String userid){
        this.mContext=mContext;
        this.mDataList=mDataList;
        this.userid = userid;
        mLayoutInflater=LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=mLayoutInflater.from(parent.getContext()).inflate(R.layout.user_photo_item,parent,false);
        return new UserPhotoListAdapter.ViewHolder(v);
    }

    //将数据绑定到控件
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final  Photo entity= mDataList.get(position);
        if (null==entity)
            return;
        UserPhotoListAdapter.ViewHolder viewHolder= (UserPhotoListAdapter.ViewHolder) holder;
        viewHolder.photo_user_name.setText(entity.getUserid());
        viewHolder.photo_time.setText(entity.getUpdate_time());
        viewHolder.photo_intent.setText(entity.getTitle());
        if(!entity.getThumb0().isEmpty()) {
            Picasso.with(mContext).load("http://172.24.10.175/workout/Uploads/"+entity.getThumb0()).into(viewHolder.photo_pic1);
        } else {
            viewHolder.photo_pic1.setImageResource(R.drawable.qya);
        }
        if(!entity.getThumb1().isEmpty()) {
            Picasso.with(mContext).load("http://172.24.10.175/workout/Uploads/"+entity.getThumb1()).into(viewHolder.photo_pic2);
        } else {
            viewHolder.photo_pic2.setImageResource(R.drawable.qya);
        }
        if(!entity.getThumb2().isEmpty()) {
            Picasso.with(mContext).load("http://172.24.10.175/workout/Uploads/"+entity.getThumb2()).into(viewHolder.photo_pic3);
        } else {
            viewHolder.photo_pic3.setImageResource(R.drawable.qya);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(mContext,PhotoInfoActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("id",entity.getId());
                intent.putExtra("title",entity.getTitle());
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    //找到控件，将其初始化
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView photo_pic1, photo_pic2, photo_pic3;
        TextView photo_user_name,photo_time, photo_intent;
        public ViewHolder(View itemView) {
            super(itemView);
            photo_user_name = (TextView) itemView.findViewById(R.id.user_photo_user_name);
            photo_pic1 = (ImageView) itemView.findViewById(R.id.user_photo_photo1);
            photo_pic2 = (ImageView) itemView.findViewById(R.id.user_photo_photo2);
            photo_pic3 = (ImageView) itemView.findViewById(R.id.user_photo_photo3);
            photo_time = (TextView) itemView.findViewById(R.id.user_photo_time);
            photo_intent = (TextView) itemView.findViewById(R.id.user_photo_intent);
        }
    }
}

